<?php

namespace common\rest\actions\myorder;
use common\models\c2\entity\SalesOrder;
use common\models\c2\statics\SalesOrderStateType;
use Yii;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\ServerErrorHttpException;
use yii\rest\Action;
use common\rest\statics\ResponseDatum;
use common\rest\base\WxException;
use common\rest\statics\OperationResult;
/**
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/25
 * Time: 10:41
 */
class UpdateAction extends Action
{
    /**
     * @var string the scenario to be assigned to the new model before it is validated and saved.
     */
    public $scenario = Model::SCENARIO_DEFAULT;

    /**
     * @var string the name of the view action. This property is need to create the URL when the model is successfully created.
     */
    public $viewAction = 'view';

    /**
     * auth id
     * @var string
     */
    public $authIdAttribute = "user_id";

    public $SalesOrderState ;

    public function run(){
        $model = new $this->modelClass;
        $params = Yii::$app->getRequest()->getBodyParams();
        if (is_null($params['id'])) {
            throw new WxException(Yii::t('app.rest', 'Resource not found!'), OperationResult::ERROR_404, null);
        }
        if(!Yii::$app->user->identity->getId()){
            throw new WxException(Yii::t('app.c2', "Missing parameter：{s1}", ['s1' => 'id']), OperationResult::ERROR_600, null, []);
        }

        if(empty($params['state'])){
            throw new WxException(Yii::t('app.c2', "Missing parameter：{s1}", ['s1' => 'type']), OperationResult::ERROR_600, null, []);
        }
        $data = $this->SalesOrderState($model,$params['id'],$params['state']);
        if ($data) {
            return ResponseDatum::getSuccessDatum(['msg' => Yii::t('app.c2', 'Operation completed successfully!')], $data);
        } else {
            throw new WxException(Yii::t('app.c2', "Missing parameter：{s1}", ['s1' => 'id']), OperationResult::ERROR_600, null, []);
        }
    }

    public function SalesOrderState(SalesOrder $model,$id,$type=[])
    {
        $data = [];
        $order = $model::findOne($id);
        if($order->customer_id != Yii::$app->user->identity->id){
            return ResponseDatum::getErrorDatum(['code' => OperationResult::ERROR_404,'msg' => Yii::t('app.rest', "{s1} Unavailable!",['s1'=>'id'])]);
        }
        if($type == SalesOrderStateType::TYPE_FINISH){
            $data = $order->setStateFinish();
        }else if ($type == SalesOrderStateType::TYPE_CANCEL) {
            $data = $order->setStateCancel();
        }else if($type == SalesOrderStateType::TYPE_CLOSE){
            $data = $order->setStateClose();
        }else{
            return ResponseDatum::getErrorDatum(['code' => OperationResult::ERROR_404,'msg' => Yii::t('app.rest', "{s1} Unavailable!",['s1'=>$type])]);
        }

        if ($data) {
            return ResponseDatum::getSuccessDatum(['msg' => Yii::t('app.c2', 'Operation completed successfully!')], $data);
        } else {
            throw new WxException(Yii::t('app.c2', "Missing parameter：{s1}", ['s1' => 'id']), OperationResult::ERROR_600, null, []);
        }
    }
}